package com.game;

import com.game.action.*;
import com.game.logic.user.login.LoginService;
import com.xs.build.server.websocket.SocketServerConfigureProperties;
import com.xs.build.server.websocket.WebSocketServer;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.List;


@ComponentScan(basePackages = {"com", "com.sdk"})
@EnableAspectJAutoProxy
@EnableAsync(proxyTargetClass = true)
@EnableCaching(proxyTargetClass = true)
@EnableScheduling
@SpringBootApplication
public class Application {
    public static final List<Class<?>> ACTION_CLASS_LIST = List.of(
            IndexAction.class,
            HomePageAction.class,
            RankingAction.class,
            FightAction.class,
            RolesActon.class,
            BagAction.class,
            MapAction.class,
            UserAccountAction.class,
            PetAction.class,
            SkillAction.class,
            UserTaskAction.class,
            UserTitleAction.class,
            UserDongFuAction.class
    );
    @Value("${socket.port}")
    private int socketPort;
    @Value("${spring.application.name}")
    private String name;
    @Resource
    private LoginService loginService;

    @Bean
    public SocketServerConfigureProperties socketServerConfigureProperties() {
        return new SocketServerConfigureProperties();
    }

    @Bean
    public WebSocketServer webSocketServer(SocketServerConfigureProperties properties) {
        return WebSocketServer.builder()
                .name(this.name)
                .port(this.socketPort)
                .actionClassList(ACTION_CLASS_LIST).properties(properties)
                .interceptor((ctx, message) -> this.loginService.interceptor(ctx, message))
                .properties(properties).build();
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(Application.class, args);

        WebSocketServer bean = app.getBean(WebSocketServer.class);
        bean.setApplicationContext(app);
        bean.start();
    }
}